﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChatDAL;

namespace ChatASP
{
    public partial class Channel : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int channelID =  int.Parse(Session["channel"].ToString());
            List<Message> messages = ChatManagement.GetChannelMessages(channelID);
            MessageList.DataSource = messages;
            MessageList.DataBind();

            List<User> users = ChatManagement.GetAllUsers();
            UsersList.DataSource = users;
            UsersList.DataBind();
            int recieverID = 0;
            if (Session["recieverID"]!=null)
            {
                recieverID = int.Parse(Session["recieverID"].ToString());
            }
            if (recieverID != 0)
            {
                //UserChatText.Visible = true;
                //UserChatButton.Visible = true;
                List<Message> userMessages = ChatManagement.GetPrivateMessages(1, recieverID);
                UserMessageList.DataSource = userMessages;
                UserMessageList.DataBind();
            }
        }

        protected void ChatButton_Click(object sender, EventArgs e)
        {
            Message newMessage = new Message()
            {
                Text = ChatText.Text,
                MessageTime = DateTime.Now,
                SenderID = 1,
                ChannelID = int.Parse(Session["channel"].ToString())
            };
            ChatManagement.CreateMessage(newMessage);
            ChatText.Text = string.Empty;
        }

        protected void MessageUser(object sender, EventArgs e)
        {
            string id = (sender as LinkButton).ClientID.Substring((sender as LinkButton).ClientID.Length - 1);
            int recieverID = int.Parse(id) + 1;
            int a;
            if (Session["recieverID"]!=null)
            {
                a = int.Parse(Session["recieverID"].ToString());
            }
            Session["recieverID"] = recieverID;
            a = int.Parse(Session["recieverID"].ToString());
        }

        protected void UserChatButton_Click(object sender, EventArgs e)
        {
            Message newMessage = new Message()
            {
                Text = UserChatText.Text,
                MessageTime = DateTime.Now,
                SenderID = 1,
                RecieverID = int.Parse(Session["recieverID"].ToString())
            };
            ChatManagement.CreateMessage(newMessage);
            UserChatText.Text = string.Empty;
        }
    }
}